package com.yjzx.config;

import com.yjzx.bean.UpdateClass;
import com.yjzx.util.FileUtil;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yjzx
 * @date 2023/10/22
 * @description
 */
public class JarBean {
    private String jarPath;
    private String jarName;
    private String printPath;
    private List<UpdateClass> classInfos;

    public String getJarPath() {
        return jarPath;
    }

    public void setJarPath(String jarPath) {
        this.jarPath = jarPath;
    }

    public String getPrintPath() {
        return printPath;
    }

    public String getJarName() {
        return jarName;
    }

    public void setJarName(String jarName) {
        this.jarName = jarName;
    }

    public void setPrintPath(String printPath) {
        this.printPath = printPath;
    }

    public List<UpdateClass> getClassInfo() {
        return classInfos;
    }

    public void setClassInfo(List<UpdateClass> classInfo) {
        this.classInfos = classInfo;
    }

    public JarBean() {
    }

    public boolean execute() {
        File printPathFile = FileUtil.getPrintPathFile(printPath);
        String jarPath = FileUtil.toJarPath(jarName, printPath);
        ClassPool pool = ClassPool.getDefault();
        boolean isJar = true;
        try {
            if (jarPath == null) {
                isJar = false;
            } else {
                pool.insertClassPath(jarPath);
            }
            List<CtClass> ctClassList = this.classInfos.stream()
                    .map(classInfo -> classInfo.updateClass(pool, printPathFile))
                    .collect(Collectors.toList());
            if (isJar) {
                jarPrint(jarPath, ctClassList, printPathFile);
            }
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    public void jarPrint(String jarPath, List<CtClass> ctClasses, File printPathFile) {

        String printPath = printPathFile.getPath();
        //复制jar包
        FileUtil.copyFileUsingFileChannels(new File(jarPath), new File(printPath + File.separator + jarName));
        for (CtClass ctClass : ctClasses) {
            //复制class文件
            String className = ctClass.getName().replace(".", File.separator) + ".class";
            File newClass = new File(printPath + File.separator + className.substring(className.lastIndexOf(".") + 1) + ".class");
            FileUtil.copyFileUsingFileChannels(new File(printPath + File.separator + className), newClass);
            //执行的修改class的命令
            FileUtil.updateJar(printPathFile, this.jarName, className);
            //善后工作，删除多余类文件
            String[] split = className.split("\\.");
            File delFile = new File(printPath + File.separator + split[0]);
            FileUtil.delFolder(newClass);
            FileUtil.delFolder(delFile);
        }
    }

    @Override
    public String toString() {
        return "YamlConfig{" +
                "jarPath='" + jarPath + '\'' +
                ", jarName='" + jarName + '\'' +
                ", printPath='" + printPath + '\'' +
                ", classInfos=" + classInfos +
                '}';
    }
}
